package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTradeMarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTradeMarkService {
    @Autowired
    private BaseCategoryTradeMarkService baseCategoryTradeMarkService;
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkList(Long category3Id) {
        //创建查询条件
        LambdaQueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //查询base_category_trademark表中数据
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //根据查询出的数据获取品牌ID的集合
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //根据id去查询品牌集合
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
            return baseTrademarkList;
        }
        return null;

    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //创建查询条件
        LambdaQueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //查询base_category_trademark表中数据
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //根据查询出的数据获取品牌ID的集合
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //查询所有品牌集合
            List<BaseTrademark> baseTrademarkAllList = baseTrademarkMapper.selectList(null);
            //过滤掉已有的品牌
            List<BaseTrademark> baseTrademarkList = baseTrademarkAllList.stream().filter(baseTrademark -> !tmIdList.contains(baseTrademark.getId())).collect(Collectors.toList());
            return baseTrademarkList;
        }else {
            //查询所有品牌集合
            List<BaseTrademark> baseTrademarkAllList = baseTrademarkMapper.selectList(null);
            return baseTrademarkAllList;
        }

    }

    /**
     * 保存分类品牌关联
     * @param categoryTrademarkVo
     */
    @Override
    public void saveBaseCategoryTradeMark(CategoryTrademarkVo categoryTrademarkVo) {
        //将品牌id封装为base_category_trademark的List集合
        List<BaseCategoryTrademark> baseCategoryTrademarkList = categoryTrademarkVo.getTrademarkIdList().stream().map(tmId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(tmId);
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        //调取方法批量添加
        baseCategoryTradeMarkService.saveBatch(baseCategoryTrademarkList);

    }
}
